package com.lordjoe.collectiveintelligence.data;

import java.util.*;

/**
 * com.lordjoe.collectiveintelligence.data.Dataset
 *
 * @author Steve Lewis
 * @date May 12, 2009
 */
public class Dataset implements IDataSet
{
    public static Dataset[] EMPTY_ARRAY = {};
    public static Class THIS_CLASS = Dataset.class;

    private final List<IDataItem> m_Items = new ArrayList<IDataItem>();
    private final Set<IMeasurementType> m_Types = new HashSet<IMeasurementType>();
    
    private final String m_Name;

    public Dataset(String pName)
    {
        m_Name = pName;
    }

    public String getName()
    {
        return m_Name;
    }

    public void addItem(IDataItem added)
    {
        IMeasurementType[] types = added.getMeasurementTypes();
        for (int i = 0; i < types.length; i++) {
            IMeasurementType type = types[i];
            m_Types.add(type);
        }
        m_Items.add(added);
    }
    public IDataItem[] getItems()
    {
       return  m_Items.toArray(IDataItem.EMPTY_ARRAY);
    }
    public IMeasurementType[] getTypes()
    {
       return  m_Types.toArray(IMeasurementType.EMPTY_ARRAY);
    }

    public <T> IMeasurementSet<T> getMeasurements(IMeasurementType<T> type)
    {
        MeasurementSet<T> set = new MeasurementSet(type);
        IDataItem[] iDataItems = getItems();
        for (int i = 0; i < iDataItems.length; i++) {
            IDataItem item = iDataItems[i];
            IMeasurement<T> m = item.getMeasurement(type);
            if(m != null)
                set.addMeasurement(m);
        }
        return set;
    }
}
